/*
 * Created on Sep 19, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package edu.mse.camel.client.whiteboard.gef.ui.freehandwriting;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.Request;
import org.eclipse.gef.SharedCursors;
import org.eclipse.gef.tools.CreationTool;
import org.eclipse.swt.graphics.Color;

import edu.mse.camel.client.whiteboard.gef.whiteboard.XY;
import edu.mse.camel.client.whiteboard.gef.whiteboard.impl.XYImpl;


public class FreeHandwritingTool extends CreationTool {

    
    private List <XY> points;
    
    private Color color;
    
    public FreeHandwritingTool(Color color)
    {
        super();
        setFactory(new FreeHandwritingToolFactory());
        setUnloadWhenFinished(false);
        setDefaultCursor(SharedCursors.CROSS);
        setDisabledCursor(SharedCursors.NO);
        this.color = color;
    }
    
    
    protected Request createTargetRequest() {
        FreeHandwritingCreateRequest request = new FreeHandwritingCreateRequest(color);
        request.setFactory(getFactory());
        return request;
    }
  
    protected FreeHandwritingCreateRequest getCreateRequest() {
        return (FreeHandwritingCreateRequest) getTargetRequest();
    }
    
     
    protected boolean handleButtonDown(int button) {
    	
        if(super.handleButtonDown(button)){
             points = new ArrayList();
            FreeHandwritingCreateRequest request = this.getCreateRequest();
            request.setPoints(points);  
         
            
            Point loc = this.getLocation();
            points.add(new XYImpl(loc.x, loc.y));
            return true;
        } else
        	return false;   
    }
   
    
    private Point prev_loc = null;
    
     protected void updateTargetRequest() {
    //	 System.out.println("FreehandSketchingTool: updateTargetRequest");
        FreeHandwritingCreateRequest req = getCreateRequest();
        
        if (isInState(STATE_DRAG_IN_PROGRESS)) {
            
            Point loc = this.getLocation();
            if(!loc.equals(prev_loc)) {
                points.add(new XYImpl(loc.x, loc.y));
           }
            prev_loc = loc;
            
        } else {
            req.setSize(null);
            req.setLocation(getLocation());
            // req.setPoints(null);
        }
    }
     
         
     protected void performCreation(int button) {
       executeCurrentCommand();
     }
        
     public void handleFinished()
     {
         super.handleFinished();
         
      }
}
